package org.ehe.erp.controller.purchase;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.idempotent.annotation.RepeatSubmit;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.erp.domain.purchase.bo.ErpPurchaseInItemsBo;
import org.ehe.erp.domain.purchase.vo.ErpPurchaseInItemsVo;
import org.ehe.erp.service.purchase.IErpPurchaseInItemsService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ERP 采购入库明细
 *
 * @author zhangnn
 * @date 2025-10-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erp/purchaseInItems")
public class ErpPurchaseInItemsController extends BaseController {

    private final IErpPurchaseInItemsService purchaseInItemsService;

    /**
     * 查询采购入库明细列表
     */
    @SaCheckPermission("erp:purchaseInItems:list")
    @GetMapping("/list")
    public TableDataInfo<ErpPurchaseInItemsVo> list(ErpPurchaseInItemsBo bo, PageQuery pageQuery) {
        return purchaseInItemsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出采购入库明细列表
     */
    @SaCheckPermission("erp:purchaseInItems:export")
    @Log(title = "采购入库明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpPurchaseInItemsBo bo, HttpServletResponse response) {
        List<ErpPurchaseInItemsVo> list = purchaseInItemsService.queryList(bo);
        ExcelUtil.exportExcel(list, "采购入库明细", ErpPurchaseInItemsVo.class, response);
    }

    /**
     * 获取采购入库明细详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("erp:purchaseInItems:query")
    @GetMapping("/{id}")
    public R<ErpPurchaseInItemsVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable Long id) {
        return R.ok(purchaseInItemsService.queryById(id));
    }

    /**
     * 新增采购入库明细
     */
    @SaCheckPermission("erp:purchaseInItems:add")
    @Log(title = "采购入库明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpPurchaseInItemsBo bo) {
        return toAjax(purchaseInItemsService.insertByBo(bo));
    }

    /**
     * 修改采购入库明细
     */
    @SaCheckPermission("erp:purchaseInItems:edit")
    @Log(title = "采购入库明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpPurchaseInItemsBo bo) {
        return toAjax(purchaseInItemsService.updateByBo(bo));
    }

    /**
     * 删除采购入库明细
     *
     * @param ids 主键串
     */
    @SaCheckPermission("erp:purchaseInItems:remove")
    @Log(title = "采购入库明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(purchaseInItemsService.deleteWithValidByIds(List.of(ids), true));
    }
}
